package com.zufe.test6;

public class Test1 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        //2.创建学生对象并添加到数组当中
        Student stu1 = new Student(1, "ZhangSan", 23);
        Student stu2 = new Student(2, "LiSi", 24);
        Student stu3 = new Student(3, "WangSan", 25);

        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;

        Student stu4 = new Student(4, "WangWu", 25);

        if (!isContains(arr, stu4)) {
            int count = getCount(arr);
            if (count == arr.length) {
                Student[] newArr = new Student[arr.length + 1];
                for (int i = 0; i < arr.length; i++) {
                    newArr[i] = arr[i];
                }
                newArr[count] = stu4;
                printArr(newArr);
            } else {
                arr[count] = stu4;
                printArr(arr);
            }
        } else {
            System.out.println("id重复");
        }
    }

    public static boolean isContains(Student[] arr, Student stu) {
        boolean flag = false;
        for (Student student : arr) {
            if (student != null) {
                if (student.getId() == stu.getId()) {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }

    public static int getCount(Student[] arr) {
        int count = 0;
        for (Student student : arr) {
            if (student != null) {
                count++;
            }
        }
        return count;
    }

    public static void printArr(Student[] arr) {
        for (Student student : arr) {
            if (student != null) {
                System.out.println("id:" + student.getId() + " name:" + student.getName() + " age:" + student.getAge());
            }
        }
    }
}
